package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.ServerHttpRequest;
import reactor.core.publisher.Mono;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class GatwayApp {
    public static void main( String[] args )
    {
        SpringApplication.run(GatwayApp.class,args);
    }

    @Bean
    @Order(0)
    public GlobalFilter a(){
        return (exchange, chain) -> {
            System.out.println("第1个过滤器");
            ServerHttpRequest request1 = exchange.getRequest();
            String first1 = request1.getHeaders().getFirst("token");
            System.out.println("末尾方法第1个过滤器 value的值为"+first1);
            return chain.filter(exchange).then(Mono.fromRunnable(()->{
                ServerHttpRequest request = exchange.getRequest();
                String first = request.getHeaders().getFirst("token");
                System.out.println("末尾方法第1个过滤器 value的值为"+first);
                System.out.println("第1个过滤器，末尾方法");
            }));
        };
    }
    @Bean
    @Order(1)
    public GlobalFilter b(){
        return (exchange, chain) -> {
            System.out.println("第2个过滤器");
            return chain.filter(exchange).then(Mono.fromRunnable(()->{
                System.out.println("第2个过滤器，末尾方法");
            }));
        };
    }
    @Bean
    @Order(2)
    public GlobalFilter c(){
        return (exchange, chain) -> {
            System.out.println("第3个过滤器");
            return chain.filter(exchange).then(Mono.fromRunnable(()->{
                System.out.println("第3个过滤器，末尾方法");
            }));
        };
    }
}
